package com.dataseek.iot.device.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.MqttTopic;
import com.dataseek.iot.mysql.service.impl.MqttTopicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.management.Query;
import java.util.List;

/**
 * @author cc
 * @Title: mqtt topic handler
 * @Description: mqtt topic handler
 * @date 2021/9/2812:24 下午
 * @since 1.0
 */
@Slf4j
@Service
public class MqttTopicHandler  extends ServiceHandler<MqttTopic> {

    @Autowired
    MqttTopicServiceImpl mqttTopicService;

    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        Boolean isSaved =  mqttTopicService.save((MqttTopic) entity);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        Boolean isSaved =  mqttTopicService.saveBatch(lists);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Boolean isRemoved =  mqttTopicService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        Boolean isRemoved = mqttTopicService.removeByIds(ids);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean isRemoved = mqttTopicService.remove(wrapper);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean isRemoved = mqttTopicService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean isUpdated = mqttTopicService.updateById((MqttTopic) entity);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean isUpdated = mqttTopicService.update(wrapper);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean isUpdated = mqttTopicService.updateBatchById(entities);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        MqttTopic p = mqttTopicService.getById(id);
        return this.createResponse(p);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page page = mqttTopicService.page(p);
        return this.createResponse(page);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page page = mqttTopicService.page(p, wrapper);
        return this.createResponse(page);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<MqttTopic>  dictProducts = mqttTopicService.listByIds(ids);
        return this.createResponse(dictProducts);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List list = mqttTopicService.list(wrapper);
        return this.createResponse(list);
    }

    @Override
    public Flux<ResponseEntity> findAll() {
        List list = mqttTopicService.list();
        return this.createResponse(list);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }

    public Flux<ResponseEntity> findByDeviceCode(String code){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("device_code", code);
        List<MqttTopic> topics = mqttTopicService.list(wrapper);
        return this.createResponse(topics);
    }

    public Mono<ResponseEntity> deleteDeviceByCode(String code){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("device_code", code);
        Boolean b = mqttTopicService.remove(wrapper);
        return this.createResponse(b);
    }
}
